<?php

namespace Operation\Controller;

use Think\Controller;

class IndexController extends ComController {

    public $checkRight = array('check' => 'all', 'uncheck' => array('default', 'admin_repwd', 'admin_repwd_act', 'navigation', 'navigation_update', 'navigation_del', 'navigation_edit', 'navigation_recycle', 'navigation_recycle_del', 'navigation_recycle_restore'));
    public $layout = 'admin';

    public function init() {
        
    }

    public function index() {

        $where = array(
            'is_del' => 0
        );
        $field = 'count(*) as count';
        $seller_amount = M('seller')->where($where)->field($field)->find();
        $this->assign('seller_amount', $seller_amount);

        $goods_amount = M('goods')->where($where)->field($field)->find();
        $this->assign('goods_amount', $goods_amount);

        $brand_amount = M('brand')->where($where)->field($field)->find();
        $this->assign('brand_amount', $brand_amount);


        $code1_num = M('code')->where(array('is_del' => 0, 'status' => 1))->count('status');
        $code2_num = M('code')->where(array('is_del' => 0, 'status' => 2))->count('status');
        $this->assign('code1_num', $code1_num);
        $this->assign('code2_num', $code2_num);
        $this->display();
    }

    public function insert() {
        $file = './Public/File/category.txt';
        $data = trim(file_get_contents($file));
        $array = explode("\r", $data);
        $arr = array();
        foreach ($array as $key => $value) {
            $arr[$key] = explode("\t", $value);
        }
        
        ob_clean();
        $Obj = M('product_cat');
        foreach ($arr as $k => $v) {
            $cat[$k] = array(
                'cat_id' => trim($v[0]) . trim($v[1]) . trim($v[2]) . trim($v[3]) . trim($v[4]),
                'cat_name' => self::icon($v[5]),
                'note' => self::icon($v[6]),
                'instruction_id' => trim($v[7]),
                'CPC' => trim($v[8])
            );
            $cat[$k]['parent_id'] = substr($cat[$k]['cat_id'], 0, -2);
          $res = $Obj->add($cat[$k]);  
          //dump($res);
        }
        //dump($res);die;
    }
    
    function icon($string){
        $encode = mb_detect_encoding($string, array('ASCII','UTF-8','GB2312','GBK','BIG5')); 
        $str = iconv($encode,"UTF-8",$string); 
        return trim($str);
    }

    
    
}